﻿<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WareHouse.aspx.cs"
    Inherits="PromotionEngine.WareHouse" Title="Promotion Engine - Warehouse" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <style type="text/css">
        .k-upload-selected
        {
            display: none;
        }
    </style>
    <script type="text/javascript">
        var oWarehouse = {};
        var isInsert = false;
        var wareHouseData;
        var gird;
        var fileUpload;
        function showDetails(e) {
            wnd.center().open();
        }

        $(document).ready(function () {
            //Load Grid
            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "Warehouse.aspx/GetAllWareHouses",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: "Warehouse.aspx/DeleteWarehouse",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {

                            if (operation === "destroy") {
                                return { warehouseID: data.WarehouseID };
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (a) {
                            if (a.d != null) {
                                var data = a.d.Data;
                                return data;
                            }
                        },
                        total: "d.Count",
                        model: {
                            id: "WarehouseID",
                            fields: {
                                WarehouseID: { type: "string", validation: { required: true} },
                                WarehouseCode: { type: "string", validation: { required: true} },
                                WarehouseName: { type: "string", validation: { required: true} },
                                CompanyCode: { type: "string" },
                                CompanyName: { type: "string" },
                                Address: { type: "string" }
                            }
                        }
                    }
                },
                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                height: 450,
                selectable: "row",
                toolbar: [{ name: 'create', text: 'Add New Wareshouse' }, { name: 'excel' }
                                   ],
                excel: {
                    fileName: "Warehouse List.xlsx",
                    filterable: true,
                    allPages: true
                },
                batch: true,
                columns: [{ field: "WarehouseID", title: "Warehouse ID", hidden: true },
                                { field: "WarehouseCode", title: "Warehouse Code", width: 300 },
                            { field: "WarehouseName", title: "Warehouse Name" },
                              { field: "Address", title: "Address", width: 300 },
                            { field: "CompanyName", title: "Company Name", width: 300 },
                            { command: ["edit", "destroy"], title: "Action", width: "170px" }
                            ],
                editable: {
                    mode: "popup",
                    template: $("#template").html(),
                    confirmation: "Are you sure you want to remove this warehouse?"
                },
                edit: function (e) {
                    var editWindow = e.container.data("kendoWindow");
                    if (e.model.isNew()) {
                        e.container.data("kendoWindow").title('Add New Warehouse');
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                        isInsert = true;
                    }
                    else {
                        e.container.data("kendoWindow").title('Edit Warehouse');
                        isInsert = false;
                    }
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oWarehouse[name] = $("[name=" + name + "]").val();
                        }
                    }

                    //Update or Insert Company
                    $.ajax({
                        type: "POST",
                        url: "Warehouse.aspx/UpdateWarehouse",
                        data: JSON.stringify({
                            oWarehouse: JSON.stringify(oWarehouse),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            if (isInsert) {
                                grid.dataSource.read();
                            }
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <div style="position: absolute; z-index: 0; margin-right: 8px;">
        <div id="grid">
        </div>
    </div>
    <script type="text/x-kendo-template" id="template">  
    <div style="width: 400px;">
        <div class="k-edit-label" style="display: none;">
            <label for="WarehouseID">
                ID</label></div>
        <div class="k-edit-field" data-container-for="WarehouseID" style="display: none;">
            <input name="WarehouseID" class="k-input k-textbox" type="text" readonly="readonly"
                data-bind="value:WarehouseID"></div>
        <div class="k-edit-label">
            <label for="WarehouseCode">
                Warehouse Code</label></div>
        <div class="k-edit-field" data-container-for="WarehouseCode">
            <input name="WarehouseCode" class="k-input k-textbox" required="required" type="text"
                data-bind="value:WarehouseCode"></div>
        <div class="k-edit-label">
            <label for="WarehouseName">
                Warehouse Name</label></div>
        <div class="k-edit-field" data-container-for="WarehouseName">
            <input name="WarehouseName" class="k-input k-textbox" type="text" data-bind="value:WarehouseName"></div>
        <div class="k-edit-label">
            <label for="Address">
                Address</label></div>
        <div class="k-edit-field" data-container-for="Address">
            <input name="Address" class="k-input k-textbox" type="text" data-bind="value:Address"></div>
    </script>
</asp:Content>
